package org.sjs.dreamfly.common.base

import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.widget.RelativeLayout
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.MainScope
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.widget.TitleBar

abstract class BaseActivity : AppCompatActivity(), UiInterface {

    protected val mainScope by lazy { MainScope() }
    private var titleBar: TitleBar? = null
    private var titleBarVisible: Boolean = true
    private var loadingView: View? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        LogUtil.d("")
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        requireLayoutId().let {
            if (it == null) {
                setContentView(null)
            } else {
                setContentView(it)
            }
        }
        titleBar?.setTitle(this.title.toString())
        onInitViewModel()
        onBindViews()
        onInitViews()
        onLoadData(savedInstanceState, null)
    }

    override fun onNewIntent(intent: Intent?) {
        LogUtil.d("")
        super.onNewIntent(intent)
        onLoadData(null, intent)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (newConfig.orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                setStatusBarVisibility(false)
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                setStatusBarVisibility(true)
            }
        }
    }

    override fun onLoadData() {
        onLoadData(null, null)
    }

    override fun setContentView(layoutResID: Int) {
        this.setContentView(LayoutInflater.from(this).inflate(layoutResID, null), null)
    }

    override fun setContentView(view: View?) {
        this.setContentView(view, null)
    }

    override fun setContentView(view: View?, params: ViewGroup.LayoutParams?) {
        val rootView = LayoutInflater.from(this).inflate(R.layout.base_activity, null) as ViewGroup
        titleBar = rootView.findViewById(R.id.title_bar)
        titleBar?.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        if (!titleBarVisible) {
            titleBar?.visibility = View.GONE
        }
        loadingView = rootView.findViewById(R.id.loading_view)
        val container = rootView.findViewById<RelativeLayout>(R.id.container)
        if (view != null) {
            container.addView(view, 0,
                RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT)
            )
        }
        val lp = params ?: ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        super.setContentView(rootView, lp)
    }

    override fun setTitle(title: CharSequence?) {
        titleBar?.setTitle(title = title.toString())
        super.setTitle(title)
    }

    fun hideTitleBar() {
        titleBarVisible = false
        if (titleBar != null) {
            titleBar?.visibility = View.GONE
            titleBar?.parent?.requestLayout()
        }
    }

    fun showTitleBar() {
        titleBarVisible = true
        if (titleBar != null) {
            titleBar?.visibility = View.VISIBLE
            titleBar?.parent?.requestLayout()
        }
    }

    fun showLoadingView() {
        loadingView?.visibility = View.VISIBLE
    }

    fun hideLoadingView() {
        loadingView?.visibility = View.GONE
    }

    @RequiresApi(Build.VERSION_CODES.R)
    fun setStatusBarVisibility(isVisible: Boolean) {
        if (isVisible) {
            window.decorView.windowInsetsController?.show(WindowInsets.Type.statusBars())
        } else {
            window.decorView.windowInsetsController?.hide(WindowInsets.Type.statusBars())
        }
    }

    abstract fun onLoadData(savedInstanceState: Bundle?, newIntent: Intent?)

}